SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE dbo.[DnnProduct_CDS_CategoriesAdd]
	@DistributorID varchar(50),
	@Name nvarchar(50),
	@Description nvarchar(512),
	@ParentID int,
	@IsReadOnly bit = 0
AS
BEGIN

	DECLARE @Path1 nvarchar(4000)

	IF (@ParentID > 0)
		SELECT @Path1 = CASE
							WHEN len([Path]) = 0 THEN Name
							ELSE [Path] + '/' + Name
						END
		FROM [DnnProduct_CDS_Categories] WHERE ID = @ParentID
	ELSE
		SET @Path1 = ''

	INSERT INTO [DnnProduct_CDS_Categories]
	(
		DistributorID,
		Name,
		[Description],
		ParentID,
		[Path],
		[IsReadOnly]
	)
	VALUES
	(
		@DistributorID,
		@Name,
		@Description,
		@ParentID,
		@Path1,
		@IsReadOnly
	)

	SELECT SCOPE_IDENTITY()
END
GO
